<?
class Disk {
    static protected $cmd_udev='/sbin/udevadm info -q property -n ';
    var $device;
    var $vendor;
    var $model;
    var $size;

    public static function isDisk($dev){
        $dtype=explode('=', exec(self::$cmd_udev.$dev.'|grep DEVTYPE='));
        if (isset($dtype) && count($dtype)>1){
            if ($dtype[1] == 'disk'){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    function getVendor(){
        $vendor=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_VENDOR='));
        if (isset($vendor) && count($vendor)>1){
            $vendor=$vendor[1];
        } else {
            $vendor='<undefined>';
        }
        return $vendor;
    }

    function getModel(){
        $model=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_MODEL='));
        if (isset($model) && count($model)>1){
            $model=$model[1];
        } else {
            $model='<undefined>';
        }
        return $model;
    }

    function getCapacity(){
        // I can't get it with udevadm... try with fdisk?
        $capacity=0;
        return $capacity;
    }

    function getPartitions(){
        // I only can get mounted partitions. Try with fdisk for better results?
        $partitions=array();
        $num=explode('=', exec(self::$cmd_udev.$this->device.'|grep UDISKS_PARTITION_TABLE_COUNT='));
        if (isset($num) && count($num)>1){
            $num=(int)$num[1];
            for ($i=0; $i<$num; $i++){
                $partitions[$i]=$this->device.($i+1);
            }
        } else {
            return false;
        }
        return $partitions;
    }

    function hasFS(){
            $fs=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_FS_USAGE='));
        if ($fs[1] == 'filesystem'){
            return true;
        } else {
            return false;
        }
    }

    function isMounted(){
        //
        // True: $this->device is mounted
        // False: $this->device is not mounted
        //
        $mounted=exec("grep '".$this->device." ' /proc/mounts && echo $?");
        $mounted=(int)$mounted;

        if ($mounted == 0){
            return True;
        } else {
            return False;
        }
    }

    function getLabel(){
        $label='undefined';
        $label=explode('=', exec(self::$cmd_udev.$this->device.'|grep ID_FS_LABEL='));
        $label=$label[1];
        return $label;
    }


    function getMountpoint(){
        //
        // returns path where '$this->device' is mounted: /mnt
        //
        $mountpoint=exec("grep '".$this->device." ' /proc/mounts |cut -d' ' -f2");
        return $mountpoint;
    }

    function umount(){
        $result=exec("/usr/bin/sudo /bin/umount ".$this->device);
    }

    function disk($device){
        $this->device=$device;
        $this->vendor=$this->getVendor();
        $this->model=$this->getModel();
        #$this->size=$this->getCapacity();
    }

}

?>
